﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DLL.Entities;
using DLL.Entities.Config;
using DLL.Entities.Event;
using DLL.Entities.Status;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Media;

namespace COM
{
    public partial class Form1 : Form
    {
        string XMLdir = "..\\..\\..\\DLL\\XML\\";
        string Logdir = "..\\..\\..\\DLL\\Logs\\";
        string mediadir = "..\\..\\..\\DLL\\Media\\";
        RADEvent target;
        COMEvent targetpackage;
        MISEvent result;

        int id, speed, altitude, heading, distance;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            btn_Fire.Enabled = false;
            btn_Abort.Enabled = false;
            btn_clear.Enabled = false;
            lbl_Alert.Visible = false;

            timer1.Start();
            timer2.Start();
            object test = new object();
            Config COMCON = new Config();

            txt_MisStatus.Text = "OFF-LINE";
            txt_MissileInfo.Text = "";
            txt_RadStatus.Text = "OFF-LINE";
            txt_TargetInfo.Text = "";

            if (File.Exists(XMLdir + "COM_Config.xml"))
            {
                try
                {
                
                    XmlSerializer serializer = new XmlSerializer(typeof(Config));
                    Read_Write.ReadXML(out test, "COM_Config.xml", serializer);
                    COMCON = (Config)test;
                
                }

                catch (Exception ex)
                {
                    Utility.LogFile(ex.ToString());
                }
                        
                txt_CurrentModel.Text = COMCON.Model;
                txt_CurrentVersion.Text = COMCON.Version;
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            
            RADStatus RADSTAT = new RADStatus();
            MISStatus MISSTAT = new MISStatus();

            if (File.Exists(XMLdir + "RAD_Status.xml"))
            {
                try
                {
                    object test = new object();
                    XmlSerializer serializer = new XmlSerializer(typeof(RADStatus));
                    Read_Write.ReadXML(out test, "RAD_Status.xml", serializer);
                    RADSTAT = (RADStatus)test;

                    TimeSpan span = DateTime.Now - RADSTAT.timestamp;
                    int diff = (int)span.TotalMilliseconds;
                    if (diff <= 3000)
                    {
                        txt_RadStatus.Text = "ON-LINE";
                    }
                    else
                    {
                        txt_RadStatus.Text = "OFF-LINE";
                    }
                }

                catch (Exception ex)
                {
                    Utility.LogFile(ex.ToString());
                    txt_RadStatus.Text = "OFF-LINE";
                }
            }
            else
            {
                txt_RadStatus.Text = "OFF-LINE";
            }




            if (File.Exists(XMLdir + "MIS_Status.xml"))
            {
                try
                {
                    object test = new object();
                    XmlSerializer serializer = new XmlSerializer(typeof(MISStatus));
                    Read_Write.ReadXML(out test, "MIS_Status.xml", serializer);
                    MISSTAT = (MISStatus)test;

                    TimeSpan span = DateTime.Now - MISSTAT.timestamp;
                    int diff = (int)span.TotalMilliseconds;

                    if (diff <= 3000)
                    {
                        txt_MisStatus.Text = "ON-LINE";
                        txt_MissileInfo.Text = "Anti-Air Missiles: " + MISSTAT.aacount + "\r\n\r\n" +
                                           "Anti-Missile Missiles: " + MISSTAT.amcount + "\r\n\r\n" +
                                           "Anti-Ship Missiles: " + MISSTAT.ascount;
                    }

                    else
                    {
                        txt_MisStatus.Text = "OFF-LINE";
                        txt_MissileInfo.Text = "";
                    }


                }
                catch (Exception ex)
                {
                    Utility.LogFile(ex.ToString());
                    txt_MisStatus.Text = "OFF-LINE";
                }
             } 
  
            else
                {
                    txt_MisStatus.Text = "OFF-LINE";
                }

        }
        
        private void timer2_Tick(object sender, EventArgs e)
        {
            
            target = new RADEvent();
            if (File.Exists(XMLdir + "RAD_Event.xml"))
            {
                try
                {
                    object test = new object();
                    XmlSerializer serializer = new XmlSerializer(typeof(RADEvent));
                    Read_Write.ReadXML(out test, "RAD_Event.xml", serializer);
                    target = (RADEvent)test;

                    File.Move((XMLdir + "RAD_Event.xml"), (Logdir+"RAD_Event" + Utility.Random(1, 10000000) + ".xml"));
                    lbl_Alert.Visible = true;
                    playSound();
                    txt_TargetInfo.Text = "Target ID: " + target.id + "\r\n" +
                                          "Size: " + target.size + "\r\n" +
                                          "Speed: " + target.speed + " kn\r\n" +
                                          "Altitude: " + target.altitude + " ft\r\n" +
                                          "Heading: " + target.heading + " degrees\r\n" +
                                          "Distance: " + target.distance + " miles\r\n";

                    id = target.id;
                    speed = target.speed;
                    altitude = target.altitude;
                    heading = target.heading;
                    distance = target.distance;

                    if (target.altitude < 20)
                    {
                        txt_Target.Text = "Ship";
                    }
                    else if (target.altitude >= 20 && target.altitude < 50)
                    {
                        txt_Target.Text = "Birds";
                    }
                    else if (target.altitude >= 50 && target.size == "small")
                    {
                        txt_Target.Text = "Missile";
                    }
                    else
                    {
                        txt_Target.Text = "Aircraft";
                    }
                    btn_clear.Enabled = true;
                    btn_Fire.Enabled = true;
                    
                }

                catch (Exception ex)
                {
                    Utility.LogFile(ex.ToString());
                }
            }
        }
 
        private void timer3_Tick(object sender, EventArgs e)
        {
            result = new MISEvent();
            if (File.Exists(XMLdir + "MIS_Event.xml"))
            {
                try
                {
                    
                    object test = new object();
                    XmlSerializer serializer = new XmlSerializer(typeof(MISEvent));
                    Read_Write.ReadXML(out test, "MIS_Event.xml", serializer);
                    result = (MISEvent)test;

                    txt_Result.Text += "Target ID: " + result.id + "     " + "Result: " + result.outcome + "\r\n";
                    timer3.Stop();

                    File.Move((XMLdir + "MIS_Event.xml"), (Logdir + "MIS_Event" + Utility.Random(1, 10000000) + ".xml"));
                    btn_Abort.Enabled = false;
                    btn_clear.Enabled = true;

                    if (result.outcome == "Intercept Failed" || result.outcome == "Intercept Aborted")
                        {

                            btn_Fire.Enabled = true;
                        }
                        else
                        {
                            txt_TargetInfo.Text = "";
                            txt_Target.Text = "";
                            timer2.Start();
                            btn_clear.Enabled = false;
                            lbl_Alert.Visible = false;
                        }


                }
                catch (Exception ex)
                {
                    Utility.LogFile(ex.ToString());
                }

                //txt_Result.Text = "Target ID: " + outcome.id + "    " + outcome.outcome;

                
            }
        }

        private void btn_Fire_Click(object sender, EventArgs e)
        {
            btn_clear.Enabled = false;
            targetpackage = new COMEvent();

            targetpackage.id = id;
            targetpackage.speed = speed;
            targetpackage.altitude = altitude;
            targetpackage.heading = heading;
            targetpackage.distance = distance;

            /*
            targetpackage.id = target.id;
            targetpackage.speed = target.speed;
            targetpackage.altitude = target.altitude;
            targetpackage.heading = target.heading;
            targetpackage.distance = target.distance;
            */
            if (txt_Target.Text == "Ship")
            {
            targetpackage.target = "Ship";
            }
            else if (txt_Target.Text == "Birds")
            {
                targetpackage.target = "Birds";
            }
            else if (txt_Target.Text == "Missile")
            {
                targetpackage.target = "Missile";
            }
            else if (txt_Target.Text == "Aircraft")
            {
                targetpackage.target = "Aircraft";
            }

            if (btn_aa.Checked)
            {
                targetpackage.missile = "Anti_Air";
            }
            else if (btn_am.Checked)
            {
                targetpackage.missile = "Anti_Missile";
            }
            else if (btn_as.Checked)
            {
                targetpackage.missile = "Anti_Ship";
            }
            else
            {
                MessageBox.Show("Please select a missile type.");
                return;
            }

            targetpackage.command = "Fire";

            timer2.Stop();
            XmlSerializer serializer = new XmlSerializer(typeof(COMEvent));
            Read_Write.WriteXML(targetpackage, "COM_Event.xml", serializer);
            
            timer3.Start();
            btn_Fire.Enabled = false;
            btn_Abort.Enabled = true;
        }

        private void btn_Abort_Click(object sender, EventArgs e)
        {
           
                targetpackage.command = "Abort";

                XmlSerializer serializer = new XmlSerializer(typeof(COMEvent));
                Read_Write.WriteXML(targetpackage, "COM_Event.xml", serializer);

                btn_Abort.Enabled = false;
                timer3.Start();
            

           
        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            txt_TargetInfo.Text = "";
            txt_Target.Text = "";
            btn_Fire.Enabled = false;
            btn_clear.Enabled = false;
            lbl_Alert.Visible = false;
            timer2.Start();

        }

        private void playSound()
        {
            SoundPlayer simpleSound = new SoundPlayer(mediadir + "genalrm.wav");
            simpleSound.Play();
        }
              
    }
}
